package worldinruin.gui.imp.gameview;

import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import worldinruin.core.ISoldier;
import worldinruin.resources.IconMap;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class DivideUnitComponent extends javax.swing.JPanel implements ChangeListener {
	private JLabel unitIconLabel;
	private JSlider unitDivideSlider;
	private ISoldier unit;
	private JLabel newTroopAmountLabel;
	private JLabel troopAmountLabel;
	private int unitAmount; 
	private int newUnitAmount;
	
	/**
	 * Component to divide a single soldier type.
	 * @param unit Soldier type.
	 * @param unitAmount Unit amount.
	 */
	public DivideUnitComponent(ISoldier unit, int unitAmount) {
		super();
		this.unit = unit;
		this.unitAmount = unitAmount;
		initGUI();
		updateSlider();
	}
	
	/**
	 * Updates slider with new amounts.
	 */
	private void updateSlider() {
		newUnitAmount = 0;
		unitDivideSlider.setMinimum(0);
		unitDivideSlider.setMaximum(unitAmount);
		unitDivideSlider.setValue(0);
		unitDivideSlider.setMinorTickSpacing(1);
		unitDivideSlider.addChangeListener(this);
	}
	
	/**
	 * Initiate GUI.
	 */
	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)this);
			this.setLayout(thisLayout);
			setPreferredSize(new Dimension(500, 500));
			{
				unitIconLabel = new JLabel();
				unitIconLabel.setIcon(IconMap.getInstance().getIcon(unit.getSoldierType(), 100, 100));
				unitIconLabel.setHorizontalAlignment(SwingConstants.CENTER);
				unitIconLabel.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
			}
			{
				unitDivideSlider = new JSlider();
			}
			{
				troopAmountLabel = new JLabel();
				troopAmountLabel.setText(unitAmount + "");
			}
			{
				newTroopAmountLabel = new JLabel();
				newTroopAmountLabel.setText("0");
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(thisLayout.createSequentialGroup()
				        .addComponent(unitIconLabel, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 0, Short.MAX_VALUE))
				    .addGroup(thisLayout.createSequentialGroup()
				        .addGap(26)
				        .addGroup(thisLayout.createParallelGroup()
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(unitDivideSlider, GroupLayout.PREFERRED_SIZE, 16, GroupLayout.PREFERRED_SIZE)
				                .addGap(0, 7, Short.MAX_VALUE))
				            .addGroup(thisLayout.createSequentialGroup()
				                .addComponent(troopAmountLabel, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
				                .addGap(0, 0, Short.MAX_VALUE))
				            .addGroup(thisLayout.createSequentialGroup()
				                .addComponent(newTroopAmountLabel, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
				                .addGap(0, 0, Short.MAX_VALUE)))
				        .addGap(51)))
				.addContainerGap(388, 388));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addComponent(unitIconLabel, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(troopAmountLabel, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(unitDivideSlider, 0, 215, Short.MAX_VALUE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(newTroopAmountLabel, 0, 95, Short.MAX_VALUE)
				.addContainerGap());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void stateChanged(ChangeEvent arg0) {
		JSlider unitSlider = (JSlider) arg0.getSource();
		
		unitAmount = unitSlider.getMaximum() - unitSlider.getValue();
		newUnitAmount = unitSlider.getValue();
		troopAmountLabel.setText(unitAmount + "");
		newTroopAmountLabel.setText(newUnitAmount + "");
	}

	/**
	 * Gets first troop's amount.
	 * @return First troop's amount.
	 */
	public int getUnitAmount() {
		return unitAmount;
		
	}

	/**
	 * Gets second troop's amount.
	 * @return Second troop's amount.
	 */
	public int getNewUnitAmount() {
		return newUnitAmount;
	}
	
	/**
	 * Gets soldier type.
	 * @return Soldier type.
	 */
	public ISoldier getUnit() {
		return unit;
	}
}
